package org.web.ssm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web.ssm.dao.TopicMapper;
import org.web.ssm.model.Product;
import org.web.ssm.model.Topic;
import org.web.ssm.model.queryVo.BaseQuery;
import org.web.ssm.service.ITopicService;

/**
 * Created by hubin on 2016/6/19.
 */
@Service("topicService")
public class TopicServiceimpl implements ITopicService {


    @Autowired
    private TopicMapper topicMapper;
    @Override
    public BaseQuery<Topic> getTopicList(Integer managerId, int pageSize, int pageCurrent, String keyword) throws Exception {
        BaseQuery<Topic> topicQuery = new BaseQuery<>();
        topicQuery.setPageSize(pageSize);
        topicQuery.setMaxRow(pageSize * pageCurrent + 1);
        topicQuery.setMinRow(pageSize * (pageCurrent - 1));
        topicQuery.setCurrentPage(pageCurrent);
        topicQuery.setIntkeyWords(managerId);
        topicQuery.setKeyWords(keyword);
        topicQuery.setResults(topicMapper.getTopicList(topicQuery));
        topicQuery.setTotalRow(topicMapper.getCount(topicQuery));
        int totalPage = topicQuery.getTotalRow() % topicQuery.getPageSize() == 0 ? topicQuery.getTotalRow() / topicQuery.getPageSize() : topicQuery.getTotalRow() / topicQuery.getPageSize() + 1;
        topicQuery.setTotalPage(totalPage);
        return topicQuery;
    }

    @Override
    public void addTopic(Topic topic) throws Exception {
        topicMapper.addTopic(topic);
    }

    @Override
    public void updateTopic(Topic topic) throws Exception {
        topicMapper.updateTopic(topic);
    }

    @Override
    public void delTopic(int id) throws Exception {
        topicMapper.delTopic(id);
    }
}
